//
//  AlbumImageCell.m
//  ProjectSource_Demo
//
//  Created by Liu on 16/2/1.
//  Copyright © 2016年 AngryBear. All rights reserved.
//

#import "AlbumImageCell.h"

@interface AlbumImageCell ()

@end

@implementation AlbumImageCell

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"selected" context:nil];
}

- (void)layoutSubviews
{
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.selectedImageView];
}

- (void)didMoveToSuperview
{
    [self addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    self.imageView.image = nil;
}

#pragma mark - KVO handle
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if (self.selected) {
        self.selectedImageView.image = [UIImage imageNamed:@"album_selected"];
    }
    else {
        self.selectedImageView.image = [UIImage imageNamed:@"album_unselected"];
    }
}

#pragma mark - Property Methods

- (UIImageView *)imageView
{
    if (!_imageView) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        imageView.backgroundColor = self.contentView.backgroundColor;

        _imageView = imageView;
    }
    return _imageView;
}

- (UIImageView *)selectedImageView
{
    if (!_selectedImageView) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.backgroundColor = [UIColor clearColor];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        imageView.image = [UIImage imageNamed:@"album_unselected"];
        [imageView sizeToFit];
        CGRect frame = imageView.frame;
        frame.origin.y = 5;
        frame.origin.x = self.contentView.bounds.size.width - 5 - 18;
        imageView.frame = frame;
        _selectedImageView = imageView;
    }
    return _selectedImageView;
}

@end
